package cn.tedu.di;

import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;

//测试自己实现DI--模拟@Autowired注解
public class TestMyDI {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("cn.tedu.id.Teacher");
        //获取Student类里的所有属性(name/teacher)
        Field[] fs = c.getDeclaredFields();//暴力反射
        // 遍历得到每个属性f
        for (Field f : fs) {
        //判断哪个属性有@Autowired注解,就给哪个属性设置值new Teacher();
            Autowired a = f.getAnnotation(Autowired.class);
            if (a !=null){//有注解
               Object obj = c.newInstance();
               f.setAccessible(true);//开启私有也可以访问的权限
               f.set(obj,new Teacher());//给属性设置值
            }
        }

    }
}
